package com.shuoyue.ycgk.ui.papergroups.running;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Paper;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.entity.course_teacher.AppPrice;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.papergroups.CreatePagerSetContract;
import com.shuoyue.ycgk.ui.papergroups.adapter.PaperAdapter;
import com.shuoyue.ycgk.ui.pay.PreRushOrderActivity;
import com.shuoyue.ycgk.ui.questionbank.practice.paperset.questionitem.DoPaperActivity;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;
import com.shuoyue.ycgk.views.dialog.pricedialog.PriceDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class RunningInfoActivity extends BaseMvpActivity
        implements RushContract.View, CreatePagerSetContract.View {
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.free)
    TextView free;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.price_org)
    TextView priceOrg;
    @BindView(R.id.lay_price_org)
    FrameLayout layPriceOrg;
    @BindView(R.id.lay_price)
    LinearLayout layPrice;
    @BindView(R.id.setNum)
    TextView setNum;
    @BindView(R.id.questionTotal)
    TextView questionTotal;
    @BindView(R.id.recomend_recycleView)
    RecyclerView recomendRecycleView;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.buy)
    Button buy;
    @BindView(R.id.lay_buy)
    LinearLayout layBuy;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.lay_empty)
    FrameLayout layEmpty;


    PaperAdapter paperAdapter;
    PaperSet paperSet;
    protected CreatePagerSetContract.Presenter createPresenter;


    int moduleId = -1;
    int subjectTypeId = -1;

    RushContract.Presenter rushPresenter;


    public static void start(Context sourse, int moduleId, int subjectTypeId) {
        sourse.startActivity(new Intent(sourse, RunningInfoActivity.class)
                .putExtra("moduleId", moduleId)
                .putExtra("subjectTypeId", subjectTypeId));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rush_info;
    }

    @Override
    public void initData() {
        moduleId = getIntent().getIntExtra("moduleId", -1);
        subjectTypeId = getIntent().getIntExtra("subjectTypeId", -1);

        rushPresenter = new RushContract.Presenter();
        rushPresenter.attachView(this);
        rushPresenter.getRushInfo(moduleId);
        createPresenter = new CreatePagerSetContract.Presenter();
        createPresenter.attachView(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        rushPresenter.getRushInfo(moduleId);
    }

    @Override
    protected void initView() {
        pageTitle.setText("详情");
        title.setText("正在加载...");
        recomendRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        paperAdapter = new PaperAdapter(null);
        recomendRecycleView.setAdapter(paperAdapter);
        paperAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (paperSet.getIsPay() == 1 || paperSet.getIsFree() == 1) {
                Paper paper = (Paper) adapter.getItem(position);
                createPaper(paper);
            } else {
                HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "提示", "请先购买题包");
                hintConfirmDialog.setOkListener(v -> {
                    if (paperSet != null) {
                        paperSet.setModuleId(moduleId);
                        if (paperSet.getProductPriceList() == null) {
                            PreRushOrderActivity.start(mContext, paperSet,
                                    paperSet.getDefaultPrice().getPrice(),
                                    paperSet.getDefaultPrice().getId(), paperSet.getSubjectTypeId());
                        } else {
                            PriceDialog priceDialog = new PriceDialog(mContext, paperSet.getProductPriceList());
                            priceDialog.addSelectCall(speed -> PreRushOrderActivity.start(mContext, paperSet,
                                    speed.getPrice(),
                                    speed.getId(), paperSet.getSubjectTypeId()));
                            priceDialog.show();
                        }
                    }
                    hintConfirmDialog.dismiss();
                });
                hintConfirmDialog.show();
            }
        });

    }

    @OnClick({R.id.back,
            R.id.service,
            R.id.share,
            R.id.buy}
    )
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.service:
//                CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");
//                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constant.serviceUrl)));
                if (Constant.isOpenServiceExploer) {
                    Uri uri = Uri.parse(Constant.serviceUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } else {
                    CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");

                }

                break;
            case R.id.share:
                if (paperSet != null) {
                    ShareUtil.shareUrl(this, ShareUtil.RUSH_URL + moduleId,
                            paperSet.getTitle(),
                            paperSet.getBrief(),
                            null);
                }
                break;
            case R.id.buy:
                if (paperSet != null) {
                    if ((paperSet.getIsPay() == 1
                            || paperSet.getIsFree() == 1) &&
                            (paperAdapter != null && paperAdapter.getData().size() > 0)) {
                        Paper paper = paperAdapter.getItem(0);
                        createPaper(paper);
                    } else {


                        paperSet.setModuleId(moduleId);
                        if (paperSet.getProductPriceList() == null) {
                            PreRushOrderActivity.start(mContext, paperSet,
                                    paperSet.getDefaultPrice().getPrice(),
                                    paperSet.getDefaultPrice().getId(), subjectTypeId);
                        } else {
                            PriceDialog priceDialog = new PriceDialog(mContext, paperSet.getProductPriceList());
                            int finalSubjectTypeId = subjectTypeId;
                            priceDialog.addSelectCall(speed -> PreRushOrderActivity.start(mContext, paperSet,
                                    speed.getPrice(),
                                    speed.getId(), finalSubjectTypeId));
                            priceDialog.show();
                        }
                    }
                }
                break;
        }
    }

    void createPaper(Paper paper) {
        createPresenter.createSetPaper(paper.getId(), paper.getName());
    }

    @Override
    public void createPaperSuc(UserTestPaper userTestPaper) {
        DoPaperActivity.start(mContext, userTestPaper);
    }

    @Override
    public void setPaperSetInfo(PaperSet paperInfo) {
        if (paperInfo == null) {
            title.setText("暂无数据");
        } else {
            layEmpty.setVisibility(View.GONE);

            paperInfo.setIntroduce(paperInfo.getIntroduce());
            this.paperSet = paperInfo;
            setNum.setText(String.valueOf(paperInfo.getTotalPaper()));
            questionTotal.setText(String.valueOf(paperInfo.getTotalQuestion()));
            name.setText(paperInfo.getTitle());
            paperAdapter.resetData(paperSet.getTopicTestDTOS());
            if (paperInfo.getIsPay() == 1) {
                buy.setText("开始练习");
            }
            if (paperInfo.getIsFree() == 1 || paperInfo.getDefaultPrice() == null) {
                free.setVisibility(View.VISIBLE);
                layPrice.setVisibility(View.GONE);
                layBuy.setVisibility(View.GONE);
            } else {
                AppPrice appPrice = paperInfo.getDefaultPrice();

                layPriceOrg.setVisibility(TextUtils.isEmpty(appPrice.getOriginalPrice())
                        ? View.GONE : View.VISIBLE);

                price.setText("￥" + appPrice.getPrice());
                priceOrg.setText("￥" + appPrice.getOriginalPrice());
            }
        }
    }

}
